package usersObjects;

import questionsObjects.*;
import serializableObjects.*;

import java.util.ArrayList;
import java.util.List;

import systemMethods.*;


public class User {
    private final String userLogin;
    private final String userPassword;
    private String userScore;
    private Questions storageOfQuestions;
    private List<UserBehavior> listOfUserBehavior;
    private QuestionHistories questionHistories;

    public User(String userLogin, String userPassword) {
        this.userLogin = userLogin;
        this.userPassword = userPassword;
        this.userScore = new String();
        this.listOfUserBehavior = new ArrayList<UserBehavior>();
        this.questionHistories = new QuestionHistories();
    }

    public String getUserLogin() {
        return userLogin;
    }

    public boolean checkUserPassword(UserCertificate currentUserCertificate) {
        return verifyUserCertificate(currentUserCertificate) ? true : false;
    }

    public void setQuestionsForUser(Questions storageOfQuestions, UserCertificate currentUserCertificate) {
        if (verifyUserCertificate(currentUserCertificate)) {
            this.storageOfQuestions = storageOfQuestions;
        }
    }

    public Questions getQuestionsForUser(UserCertificate currentUserCertificate) {
        Questions questions = new Questions();
        if (verifyUserCertificate(currentUserCertificate)) {
            questions = storageOfQuestions;
        }
        return questions;
    }

    public void setUserBehavior(UserBehavior currentUserBehavior, UserCertificate currentUserCertificate) {
        if (verifyUserCertificate(currentUserCertificate)) {
            listOfUserBehavior.add(currentUserBehavior);
        }
    }

    public SerializableUser getSerializableUser(UserCertificate currentUserCertificate) {
        SerializableUser serializableUser;
        serializableUser = new SerializableUser("denial of access " + userLogin, "", "");

        if (verifyUserCertificate(currentUserCertificate)) {
            serializableUser = new SerializableUser(userLogin, userPassword, userScore);
        }

        return serializableUser;
    }

    public String getScore(UserCertificate currentUserCertificate) {
        String userScore = "denial of access " + userLogin;
        if (verifyUserCertificate(currentUserCertificate)) {
            userScore = this.userScore;
        }
        return userScore;
    }

    public void setScore(UserCertificate currentUserCertificate, String userResult) {
        if (verifyUserCertificate(currentUserCertificate)) {
            this.userScore = this.userScore.concat(userResult);
        }
    }

    public void setQuestionHistories(UserCertificate currentUserCertificate, QuestionHistory questionHistory) {
        if(verifyUserCertificate(currentUserCertificate)) {
            questionHistories.push(questionHistory);
        }
    }

    public QuestionHistories getQuestionHistories(UserCertificate currentUserCertificate) {
        return questionHistories;
    }

    public void executeUserBehaviors(StorageOfUsers currentStorageOfUser, UserCertificate currentUserCertificate) {
        int i;
        int numberOfUserBehavior;
        String answerFromConsole;
        boolean isBreak;

        while (true) {
            i = 0;
            if (verifyUserCertificate(currentUserCertificate)) {
                for (UserBehavior s : listOfUserBehavior) {
                    System.out.println(i++ + ") " + s.getDescriptionBehavior());
                }
                System.out.println("Please, enter the number of the required behavior");
                answerFromConsole = TestingSystemUtils.readFromConsole();
                numberOfUserBehavior = Integer.parseInt(answerFromConsole);
                isBreak = listOfUserBehavior.get(numberOfUserBehavior).executeBehavior(currentStorageOfUser, currentUserCertificate);
                if(isBreak) {
                    break;
                }
            }
        }
    }

    private boolean verifyUserCertificate(UserCertificate currentUserCertificate) {
        boolean isValid = false;
        if (userPassword.equals(currentUserCertificate.getUserPassword()) && userLogin.equals(currentUserCertificate.getUserLogin())) {
            isValid = true;
        }
        return isValid;
    }

}
